package com.rj.bd.sys.manage.user;
/**
 * @desc 用户管理模块M层的接口的实现类
 * @author 郭瑞博
 * @datetime 2024-12-26 11:00
 */
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.bd.common.entity.User;
import com.rj.bd.sys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @return
     * @desc 1.查询所有(分页加条件查询)
     */
    @Override
    public IPage<User> queryUserPage(int pageNum, int pageSize, User user) {
        System.err.println("UserServiceImpl -> getPage");
        Page<User> page = new Page<User>(pageNum, pageSize);  //设置当前页和每页显示的条数
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();//实例化条件构造器
        if (user!=null)
        {
            if(user.getUsername()!=null)//如果留言内容不为空，将其设置到条件构造器中
            {
                queryWrapper.like("username", user.getUsername());
            }
        }
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);//留言信息展示与分页
        return userPage;
    }

    /**
     * @desc 2.添加用户
     * @param user 待添加的对象
     * @return Map
     */
    @Override
    @Transactional
    public String addUser(User user) {
        System.err.println("UserServiceImpl -> addUser");

        boolean fool = this.save(user);
        return fool ? "添加成功" : "添加失败";
    }

    /**
     * @desc 3.修改用户
     * @param user 待修改的对象
     * @return Map
     */
    @Override
    @Transactional
    public String updateUser(User user) {
        System.err.println("UserServiceImpl -> updateUser");

        boolean fool = this.updateById(user);
        return fool ? "修改成功" : "修改失败";
    }

    /**
     * @desc 4.通过id删除用户
     * @param id 待删除的本条记录id
     * @return Map
     */
    @Override
    @Transactional
    public String deleteUser(Integer id) {
        System.err.println("UserServiceImpl -> deleteUser");

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUid, id);

        boolean fool = this.remove(lambdaQueryWrapper);
        return fool ? "删除成功" : "删除失败";
    }


}
